
local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local HomeGui=UIManager.ui_list.HomeGui;
local QuestGui=UIManager:CreateUI("QuestGui",require(script.QuestGui))

local Tool=require(script.Parent.tool)
local gCfg=Tool.GetConfg()

local LanguageHelper=_G.LanguageHelper;
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local FunctionList = ClientOHOH.load.FunctionList();
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local EventList = ClientOHOH.load.EventList();
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent
    self.needTime=gCfg.OnlineTime;
    self.canReceive =false
    self.dataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.activityData = self.dataBase:GetData("activity_2");


    CallBackManager:AddRbxConnect(self,HomeGui.insts.Quest.button.Activated, function()
        QuestGui:Show();
	end);

    CallBackManager:AddRbxConnect(self,QuestGui.ReceiveEvent, function()
       if  self.canReceive  then --and self:Invoke(FunctionList.open_opegg)
         self:Invoke(FunctionList.distribute_rewards)
         self.canReceive =false
         QuestGui:Claimed();
         GlobalTipGui:Show(LanguageHelper:GetText(30007));
         self:TaskDo();
        else
            GlobalTipGui:Show(LanguageHelper:GetText(530003));
       end
	end);

    CallBackManager:AddRbxConnect(self,QuestGui.BuyEvent, function()
       self.player.RobuxBuyEvent:Action(gCfg.goodsId)
     end);
   
  
     local min= math.floor(gCfg.OnlineTime/60)

    QuestGui:Init(min,gCfg.price,gCfg.list)--所需时间，价格，奖品列表
    self:TaskDo();

end;    


function  M.TaskDo(self)
	if  self.task then
		return;
	end

	self.task=_G.CommonOHOH.load.OnLineTask().new(self.parent,self.needTime,"activity_2");

    self.taskUpdateConnect=self.task.UpdateEvent:Connect(function()
        local now,flish= self.task:GetProgress();
        QuestGui:UpdateProgress(now/flish)
        local timeStr=GeneralTool.secondsToHHMM(flish-now);
        QuestGui:UpdateTime(timeStr)   
    end)

	self.taskCompleteConnect=self.task.CompleteEvent:Connect(function()
		if self.taskCompleteConnect then
			self.taskCompleteConnect:Disconnect();
			self.taskCompleteConnect=nil;
		end

        if self.taskUpdateConnect then
			self.taskUpdateConnect:Disconnect();
			self.taskUpdateConnect=nil;
		end
        QuestGui:ClearTask()
		self.task=nil;
        self.canReceive = true;
	end)
end
function M.QuestBuyDone(self)
    if self.taskCompleteConnect then
        self.taskCompleteConnect:Disconnect();
        self.taskCompleteConnect=nil;
    end

    if self.taskUpdateConnect then
        self.taskUpdateConnect:Disconnect();
        self.taskUpdateConnect=nil;
    end
    GlobalTipGui:Show(LanguageHelper:GetText(30013));
    QuestGui:BuyDone()
    self.task=nil;
    self.canReceive = false;
    self:TaskDo();
end

function M.dtor(self)
    
    if self.taskCompleteConnect  then
		self.taskCompleteConnect:Disconnect();
		self.taskCompleteConnect=nil;
	end
    if self.taskUpdateConnect then
        self.taskUpdateConnect:Disconnect();
        self.taskUpdateConnect=nil;
    end
	if self.task then
		self.task:dtor();
    end
	M.super.dtor(self);
end;
M:RegistEvent(EventList.quest_buydone,M.QuestBuyDone)
return M;